package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"lblbc.cn/demo/config"
	"lblbc.cn/demo/controllers"
	"lblbc.cn/demo/dao"
	"lblbc.cn/demo/middleware"
	"lblbc.cn/demo/services"
)

var (
	db                 *gorm.DB                       = config.SetupDatabase()
	userRepository     dao.UserDao                    = dao.NewUserRepository(db)
	noteRepository     dao.NoteDao                    = dao.NewNoteRepository(db)
	categoryDao        dao.CategoryDao                = dao.NewCategoryDao(db)
	goodsDao           dao.GoodsDao                   = dao.NewGoodsDao(db)
	orderDao           dao.OrderDao                   = dao.NewOrderDao(db)
	jwtService         services.JWTService            = services.NewJWTService()
	userService        services.UserService           = services.NewUserService(userRepository)
	noteService        services.NoteService           = services.NewBookService(noteRepository)
	categoryService    services.CategoryService       = services.NewCategoryService(categoryDao)
	orderService       services.OrderService          = services.NewOrderService(orderDao)
	goodsService       services.GoodsService          = services.NewGoodsService(goodsDao)
	authService        services.LoginService          = services.NewAuthService(userRepository)
	loginController                                   = controllers.NewLoginController(authService, jwtService)
	userController     controllers.UserController     = controllers.NewUserController(userService, jwtService)
	noteController     controllers.NoteController     = controllers.NewNoteController(noteService, jwtService)
	categoryController controllers.CategoryController = controllers.NewCategoryController(categoryService)
	goodsController    controllers.GoodsController    = controllers.NewGoodsController(goodsService)
	orderController    controllers.OrderController    = controllers.NewOrderController(orderService, jwtService)
)

func main() {
	defer config.CloseDatabaseConnection(db)
	r := gin.Default()

	authRoutes := r.Group("/user")
	{
		authRoutes.POST("/login", loginController.Login)
		authRoutes.POST("/register", loginController.Register)
	}

	noteRoutes := r.Group("note/notes", middleware.AuthorizeJWT(jwtService))
	{
		noteRoutes.GET("", noteController.QueryNotes)
		noteRoutes.GET("/:id", noteController.QueryById)
		noteRoutes.POST("", noteController.AddNote)
		noteRoutes.PUT("/:id", noteController.ModifyNote)
		noteRoutes.DELETE("/:id", noteController.DeleteNote)
	}

	r.GET("mcdonalds/categories", categoryController.QueryCategory)

	goodsRoutes := r.Group("mcdonalds/goods")
	{
		goodsRoutes.GET("", func(c *gin.Context) {
			categoryId := c.Query("categoryId")
			goodsController.QueryGoodsByCategory(c, categoryId)
		})
		goodsRoutes.GET("/:id", func(c *gin.Context) {
			goodsId := c.Param("id")
			goodsController.QueryGoods(c, goodsId)
		})
	}

	r.GET("mcdonalds/goodsBySearch", func(c *gin.Context) {
		keyword := c.Query("keyword")
		goodsController.SearchGoods(c, keyword)
	})

	orderRoutes := r.Group("mcdonalds/orders")
	{
		orderRoutes.GET("", func(c *gin.Context) {
			orderStatus := c.Query("orderStatus")
			orderController.QueryByStatus(c, orderStatus)
		})
		orderRoutes.POST("", func(c *gin.Context) {
			orderController.CreateOrder(c)
		})
		orderRoutes.GET("/:id", func(c *gin.Context) {
			orderId := c.Param("id")
			orderController.QueryOrder(c, orderId)
		})
		orderRoutes.DELETE("/:id", func(c *gin.Context) {
			orderId := c.Param("id")
			orderController.DeleteOrder(c, orderId)
		})
	}

	r.Run(":8080")

}
